#!/usr/bin/env python
#-*- encoding:utf-8 -*-
#
#       Dials
#
#       Kévin Gomez <geek63@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

from PyQt4 import QtGui, QtCore

from libs.Config import Config, USER_CONFIG_FILE


class PrefsDial(QtGui.QDialog):
    """ Preferences Dialog """

    def __init__(self):
        super(QtGui.QDialog, self).__init__()

        self.conf = Config(USER_CONFIG_FILE)

        self.setWindowTitle(self.tr('Update Checker - Préférences'))

        self.setupUI()
        self.createActions()

    def setupUI(self):
        layout = QtGui.QGridLayout(self)

        self.check_interval_w = QtGui.QSpinBox()
        self.check_interval_w.setRange(1, 3600)
        self.check_interval_w.setValue((self.conf.getInt('check_interval')/1000)/60)
        self.check_interval_w.setSuffix(self.tr(' minutes'))

        self.notif_time_w = QtGui.QSpinBox()
        self.notif_time_w.setRange(1, 120)
        self.notif_time_w.setValue(self.conf.getInt('notif_duration')/1000)
        self.notif_time_w.setSuffix(self.tr(' secondes'))

        check_interval_l = QtGui.QLabel(self.tr('Intervalle de vérification  :'))
        notif_time_l = QtGui.QLabel(self.tr('Durée d\'affichage de la notification  :'))

        self.ok_w = QtGui.QPushButton(self.tr('Valider'))
        self.cancel_w = QtGui.QPushButton(self.tr('Annuler'))

        layout.addWidget(check_interval_l, 0, 0)
        layout.addWidget(self.check_interval_w, 0, 1)
        layout.addWidget(notif_time_l, 1, 0)
        layout.addWidget(self.notif_time_w, 1, 1)
        layout.addWidget(self.ok_w, 2, 0)
        layout.addWidget(self.cancel_w, 2, 1)

        self.setLayout(layout)

    def createActions(self):
        self.connect(self.ok_w, QtCore.SIGNAL('clicked()'), self.save)
        self.connect(self.cancel_w, QtCore.SIGNAL('clicked()'), self.reject)

    def save(self):
        self.conf.set('check_interval', self.check_interval_w.value()*60*1000)
        self.conf.set('notif_duration', self.notif_time_w.value()*1000)

        try:
            self.conf.save()
        except IOError, e:
            QtGui.QMessageBox.warning(None, self.tr('Erreur'),
                                      self.tr('Impossible de sauvegarder le '+
                                              'fichier de configuration : %s'%e))
        else:
            self.accept()

    def run(self):
        self.show()
        self.exec_()


def aboutDial():
    QtGui.QMessageBox.about(None, 'À propos',
                    '<center><b>Update Checker 0.1</b><br />'
                    'A lightweight Arch linux (pacman) updates notifier.'
                    '<br /><br />Created by '
                    '<a href="http://geek-attitude.fr.nf/">Kévin Gomez</a>.'
                    '<br /><br />'
                    '<a href="http://geek-attitude.fr.nf/">La Geek Attitude</a>'
                    '<br />'
                    'geek63@gmail.com</center>')
